home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 07 DecisionMaking Architectures / 03 Laramée / Evidence.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-18  |  3.0 KB  |  103 lines

  1. /****************************************************************
  2.  *
  3.  * Dempster-Shafer Theory
  4.  * Using belief in decision-making
  5.  *
  6.  * This code demonstrates how to combine sources of "evidence"
  7.  * into something your AI can base its decisions on.
  8.  *
  9.  ***************************************************************/
  10.  
  11. // The world states we are trying to decide between
  12. enum { eventPitcherOK = 1, eventPitcherNervous = 2, eventPitcherTired = 4, eventAllEvents = 8 };
  13.  
  14. /***************************************************************
  15.  *
  16.  * STRUCT PieceOfEvidence
  17.  *
  18.  * A simple struct containing the evidence provided by a single
  19.  * source for a single set of possibilities.  For example, a 
  20.  * source may say: "30% of my evidence points towards event A
  21.  * or event B being true".
  22.  * 
  23.  ***************************************************************/
  24.  
  25. struct PieceOfEvidence
  26. {
  27.     // The data.  We store the definition of the set of events 
  28.     // explicitly to make manipulation and computation easier
  29.     int beliefSet;
  30.     double beliefValue;
  31.  
  32.     // Construction
  33.     PieceOfEvidence();
  34.     void Clear();
  35.  
  36.     // Set intersection operation to determine whether two pieces of
  37.     // evidence corroborate each other to some degree
  38.   static int Intersection( PieceOfEvidence & a, PieceOfEvidence & b )
  39.   {
  40.         return( a.beliefSet & b.beliefSet );
  41.   }
  42.     static int Intersection( PieceOfEvidence & a, int set )
  43.     {
  44.         return( a.beliefSet & set );
  45.     }
  46.  
  47.     // Evidence combines just like probability: Bel( a AND b ) = Bel( a ) * Bel( b )
  48.   static double CombinedEvidence( PieceOfEvidence & a, PieceOfEvidence & b )
  49.     {
  50.         return( a.beliefValue * b.beliefValue );
  51.     }
  52.  
  53. };
  54.  
  55.  
  56. /****************************************************************
  57.  *
  58.  * CLASS BodyOfEvidence
  59.  *
  60.  * This class contains all of the evidence provided by a single
  61.  * source, or a combination of the evidence provided by multiple
  62.  * sources.  For example, a source may say:
  63.  *
  64.  * "My evidence shows that there is a 30% chance that A is true,
  65.  * a 40% chance that either A or B is true, and a 30% chance that
  66.  * C is true."
  67.  *
  68.  ***************************************************************/
  69.  
  70. class BodyOfEvidence
  71. {
  72.     // The data.  For simplicity, we use static storage for the 
  73.     // power set of possible events, because our pitcher can only
  74.     // be in 3 different states and thus we only require 2^3 = 8
  75.     // slots in the array.  If you have a huge event set, you may
  76.     // need a more involved data structure
  77.     PieceOfEvidence mass[ eventAllEvents ];
  78.  
  79. public:
  80.  
  81.     // Construction
  82.     BodyOfEvidence();
  83.     void Clear();
  84.     void Copy( BodyOfEvidence & source );
  85.  
  86.     // Build and store a single PieceOfEvidence
  87.     void AcceptEvidence( int set, double belief );
  88.  
  89.     // Combine the currently held beliefs and evidence with the
  90.     // evidence provided by a new source
  91.     bool Combine( BodyOfEvidence & source );
  92.  
  93.     // Compute the credibility and plausibility of an event set
  94.     double Plausibility( int set );
  95.     double Credibility( int set );
  96.  
  97.     // Helper function
  98.     void Print();
  99. };
  100.  
  101.  
  102.  
  103.